// +----------------------------------------------------------------------
// | huang [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 功能: 跨店消费类
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /*
   * 获取请求店的跨店消费情况（其他店顾客在请求店消费）
   */
  async getOtherShopGuestOrderAction () {
  	// 查询所有订单
  	let data = this.get()
  	let time = data.times ? JSON.parse(data.times) : ''
  	let where = {}
  	if (time && (time[0] != null && time[1] != null)) {
		let begintime = Math.floor(new Date(time[0]).setHours(0,0,0,0)/1000)
		let endtime = Math.floor(new Date(time[1]).setHours(0,0,0,0)/1000) + 86400 // 当天加上一天
		where['o.addtime'] = {'>=': begintime, '<': endtime}
	}
	where['o.status'] = ['!=', 0]
	where['o.departmentid'] = this.shop_userinfo.departmentid
	where['g.departmentid'] = ['not in', [0, this.shop_userinfo.departmentid]]

  	let orderList = await this.model('or_shop_order')
  	.alias('o')
  	.field('o.*, g.name, g.phone, g.vipcard, g.headurl, d.id as shopid, d.name as shopname')
  	.join('`think_or_guest` g on o.guestid = g.id')
  	.join('`think_or_department` d on g.departmentid = d.id')
  	.where(where)
  	.select()
  	return this.json(orderList)
  }


  /*
   * 获取请求店的跨店消费情况（当前店顾客在其他店消费）
   */
  async getGuestOrderInOtherShopAction () {
  	let data = this.get()
  	let time = data.times ? JSON.parse(data.times) : ''
  	let where = {}
  	if (time && (time[0] != null && time[1] != null)) {
		let begintime = Math.floor(new Date(time[0]).setHours(0,0,0,0)/1000)
		let endtime = Math.floor(new Date(time[1]).setHours(0,0,0,0)/1000) + 86400 // 当天加上一天
		where['o.addtime'] = {'>=': begintime, '<': endtime}
	}
	where['o.status'] = ['!=', 0]
	where['g.departmentid'] = this.shop_userinfo.departmentid
	where['o.departmentid'] = ['!=', this.shop_userinfo.departmentid]

  	let orderList = await this.model('or_shop_order')
  	.alias('o')
  	.field('o.*, g.name, g.phone, g.vipcard, g.headurl, d.id as shopid, d.name as shopname')
  	.join('`think_or_guest` g on o.guestid = g.id')
  	.join('`think_or_department` d on o.departmentid = d.id')
  	.where(where)
  	.select()
  	return this.json(orderList)
  }

}